package cn.lyf.controller;

import cn.lyf.entity.Comment;
import cn.lyf.entity.User;
import cn.lyf.respjson.RespJson;
import cn.lyf.service.CommentService;
import cn.lyf.util.DateFormat;
import cn.lyf.util.SessionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Controller
@RequestMapping("/api/user/comment")
public class CommentController {
    @Autowired
    CommentService commentService;


    //评论博客
    //blogid:被评论的博客id
    //content:评论的内容
    @PostMapping("/commentOnBlog/{blogid}")
    @ResponseBody
    public Map<String,Object> commentOnBlog(@PathVariable("blogid")String blogid,String content){
//        System.out.println("content=="+content);
        System.out.println("blogid=="+blogid);
        User user = SessionUtils.getUser();//获取登录用户
        if(user==null)return RespJson.pack(401,"未登录不可评论",null);
        Comment comment = new Comment();
        //设置发表评论的人的网名
        comment.setContent(content);
        comment.setNickname(user.getNickname());
//        comment.setContent(content);
        //设置发表评论时间
        comment.setTcreate(DateFormat.dateFormat());
        comment = commentService.commentOnBlog(blogid, comment);
        return RespJson.pack(200,"评论成功",comment);
    }



    //回复评论或回复别人的回复
    //commentid:被回复的评论id
    //replayid:被回复的回复id
    @PostMapping("/replay2Comment/{commentid}/{replayid}")
    @ResponseBody
    public Map<String,Object> replay2Comment(@PathVariable("commentid")String commentid,@PathVariable("replayid")String replayid, String content){
        System.out.println("content=="+content);
        User user = SessionUtils.getUser();//获取登录用户
        if(user==null)return RespJson.pack(401,"未登录不可评论",null);
        return RespJson.pack(200,"评论成功", commentService.replay2Comment(commentid,replayid,content,user));
    }

    //获取某个评论的所有回复
    @GetMapping("/getReplays/{commentid}")
    @ResponseBody
    public Map<String,Object> getReplays(@PathVariable("commentid")String commentid){

        return RespJson.pack(200,"评论成功", commentService.findReplaysByCommentid(commentid));
    }

   //删除某个评论
    @PostMapping("/delOneComment/{blogid}")
    @ResponseBody
    public Map<String,Object> delOneComment(@PathVariable("blogid")String blogid,@RequestBody Comment comment){
        System.out.println("blogid=="+blogid);
        commentService.delOneComment(blogid,comment);
        return RespJson.pack(200,"", null);
    }






}
